<?php
class Admin_OverseerController extends Ctrl_Admin{
	public function indexAction(){
		$tDIMO = new DepartmentinfoModel;
		$tEMD = new ExamineModel;
		$tESMD = new ExaminescoreModel;
		$tERMD = new ExamineregionModel;
		$tOSMD = new OverseerModel; 
        $tAMO = new AdminModel;

		$time = date('Y-m-d',time());
		$tDIDatas = $tDIMO->field('num,introduce')->fList();
		foreach ($tDIDatas as $key => $Datas) {
			$tSql = 'select id,status,department,max(date),overseer,hygiene,room,toilet,checktime from '.$tOSMD->table.' where department='.$Datas['num'];
			$data = $tOSMD->query($tSql);
			$data[0]['department'] = $Datas['num'];
			$data[0]['introduce'] = $Datas['introduce'];
			$tDIData[] =$data;
		}
        foreach($tDIData as $tK=>$tV){
            $tAData = $tAMO->field('name')->where('id = '.$tV[0]['overseer'])->fRow();
            $tDIData[$tK][0]['createuser'] = $tAData['name'];
        }
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tDIData',$tDIData);
	}	
	public function addAction(){
		$p  = $_REQUEST;
        $pId = empty($p['ID'])?'':Tool_Fnc::safe_string($p['ID']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
		$tEMD = new ExamineModel;
		$tERMD = new ExamineregionModel; 
		$tECore = new ExaminescoreModel;
		$tDIMO = new DepartmentinfoModel;
			$tSql = 'select er.id,e_id,exam_content,exam_title,exam_stand,exam_score,exam_picture,e.region from '.$tEMD->table.' er left join '.$tERMD->table.' e on e.id = er.e_id ';
		$tEDatas = $tEMD->query($tSql);

		$tDIDatas= $tDIMO->field('num,introduce')->fList();
		foreach($tEDatas as $key => $val){
				$score = $tECore->field('*')->where('e_id='.$val['id'].' and x_id='.$pId)->fRow();
				if (!empty($score)) {
					$val['pingfen'] = $score['score'];
				}
				$tDatas[$val['region']][] = $val;
		}
        $this->assign('pId',$pId);
		$this->assign('tDatas',$tDatas);
		$this->assign('tDIDatas',$tDIDatas);
		$this->assign('pDepartment',$pDepartment);
	}
	public function detailAction(){
		$p = $_REQUEST;
		$tEMD = new ExamineModel;
		$tERMD = new ExamineregionModel;
		$tOMD = new OverseerModel;
		$tDIMO = new DepartmentinfoModel;
		$tAMD = new AdminModel;
        $department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);

		$tSql = 'select o.id,o.department,date,hygiene,room,toilet,a.name from '.$tOMD->table.' o left join '.$tAMD->table.' a on o.overseer=a.id where o.id='.$pId.' order by o.id desc';
		$tDIDatas = $tDIMO->field('introduce')->where('num='.$department)->fList();
		$tDatas = $tOMD->query($tSql);
		$time = date('Y-m-d',time());
		if (empty($tDatas)) {
			$tDatas[0]['department'] = $department;
			$tDatas[0]['date'] = "暂无";
			$tDatas[0]['hygiene'] = "暂无";
			$tDatas[0]['room'] = "暂无";
			$tDatas[0]['toilet'] = "暂无";
			$tDatas[0]['status'] = "0";
		}else{
		$datetime = strtotime($time);
		foreach ($tDatas as $key => $Datas) {
				$Otime = strtotime($Datas['date'])-$datetime;
				if ($Otime==0) {
					$tDatas[$key]['status'] = empty($Datas['toilet'])||empty($Datas['room'])||empty($Datas['hygiene'])?'0':'1';
				}else{
					$tDatas[$key]['status'] = '1';
				}
			}
		}
		$this->assign('tDIDatas',$tDIDatas);
		$this->assign('tDatas',$tDatas);
	}
	public function overaddAction(){
		$p = $_REQUEST;
		$tESMD = new ExaminescoreModel;
		$tOSMD = new OverseerModel;
		$time = date('Y-m-d',time());
		$department = empty($p['depa'])?'0':Tool_Fnc::safe_string($p['depa']);
		$status = empty($p['status'])?'':Tool_Fnc::safe_string($p['status']);
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
		$score = $p['sum'];
		$Judge = count($score);
		$core = count($score);
		/*$OVERSEER = $tOSMD->field('id,hygine,room,toilet')->where('department='.$department.' and overseer='.$$_SESSION['admin']['id'].' and date='.$time)->fList();*/
		if ($Judge==$core) {
				
				if ($status==3) {
						$os='toilet';

					}else if ($status==2){
						$os='hygiene';
					}else{
						$os='room';
					}
					$toilet = 100-array_sum($score);
					$tSql = 'select id,department,hygiene,room,toilet from '.$tOSMD->table.' where department='.$department.' and date="'.$time.'" and overseer='.$_SESSION['admin']['id'];
					$tODatas = $tOSMD->query($tSql);
					if (!empty($tODatas)) {
						if (!empty($tODatas[0][$os])) {
							// $Odata = array(
							// 	'department' => $department,
							// 	'date' => $time,
							// 	"$os" => $toilet,
							// 	'checktime' => $time,
							// 	'overseer' => $_SESSION['admin']['id'],
							// 	'status' => 2,
							// 	);
							// if (!$tOSMD->insert($Odata)) {
							// 	Tool_Fnc::ajaxMsg("提交失败");
							// }
							Tool_Fnc::ajaxMsg("此卫生今日已提交",0);die;
						}else{
							$tOSMD->begin();
							$Odata = array(
								'department' => $department,
								"$os" => $toilet,
								'checktime' => $time,
								'id' => $tODatas[0]['id'],
								'sta' => $status,
								);
							$tOSMD->update($Odata);	
						}
					}else{
						$Odata = array(
						'department' => $department,
						'date' => $time,
						"$os" => $toilet,
						'checktime' => $time,
						'overseer' => $_SESSION['admin']['id'],
						'status' => 2,
						);
						if (!$tOSMD->insert($Odata)) {
							$tOSMD->back();
							Tool_Fnc::ajaxMsg("提交失败");
						}
					}
			$sql = 'select max(id) from '.$tOSMD->table.' where department='.$department.' and date="'.$time.'" and overseer='.$_SESSION['admin']['id'];
			$ODatas = $tOSMD->query($sql);
			foreach ($score as $key => $val) {
				$data = array(
						'e_id'=>$key,
						'times'=>1,
						'score'=>$val,
						'datetime'=>$time,
						'o_id'=>$department,
						'x_id'=>$ODatas[0]['max(id)'],
						);
					$tESMD->insert($data);	
					$num++;
			}
			if ($num==$Judge) {
				$tOSMD->commit();
				Tool_Fnc::ajaxMsg('提交成功001','1');	
			}else{
				$tOSMD->back();
				Tool_Fnc::ajaxMsg('提交失败');	
			}

		}else{
			Tool_Fnc::ajaxMsg('请选择分数');	
		}
	    }	

	}
	public function detailsAction(){
		$q = $_REQUEST;
		$tOMD = new OverseerModel;
		$tESMD = new ExaminescoreModel;
		$tERMD = new ExamineregionModel;
		$tEMD = new ExamineModel;
        $tDIMO = new DepartmentinfoModel;

		$OID = empty($q['ID'])?'':Tool_Fnc::safe_string($q['ID']);
		$status = empty($q['status'])?'':Tool_Fnc::safe_string($q['status']);
        $department = empty($q['department'])?'':Tool_Fnc::safe_string($q['department']);
        $tDate = empty($q['tDate'])?'':Tool_Fnc::safe_string($q['tDate']);

        $tDepartment = $tDIMO->field('introduce')->where('num = '.$department)->fRow();
        
        $tEDatas = $tEMD->field('id,e_id,exam_score,exam_title,exam_stand,exam_content')->fList();
		foreach ($tEDatas as $key => $Datas) {
			$tDAta = $tESMD->field('score,x_id')->where('e_id='.$Datas['id'].' and x_id='.$OID)->fRow();
			$tEDatas[$key]['score'] = $tDAta['score'];
			$tEDatas[$key]['x_id'] = $tDAta['x_id'];
			$DATAS[$Datas['e_id']][] =  $tEDatas[$key];
		}
        $this->assign('department',$tDepartment['introduce']);
        $this->assign('tDate',$tDate);
		$this->assign('OID',$OID);		
		$this->assign('DATAS1',$DATAS[1]);
		$this->assign('DATAS2',$DATAS[2]);
		$this->assign('DATAS3',$DATAS[3]);
		$this->assign('status',$status);
	}

}
